package controllers

import (
	"news/common/response"
	"news/constStatus"
	"news/logic"
	"news/models"

	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"

	"github.com/gin-gonic/gin"
)

// 创建评论
func CommentHandler(c *gin.Context) {
	p := new(models.Comment)
	if err := c.ShouldBindJSON(p); err != nil {
		zap.L().Error("create Comment with invalid param", zap.Error(err))
		errors, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMessage(err.Error(), c)
			return
		}
		response.FailWithMessage(removeTopStruct(errors.Translate(trans)), c)
		return
	}
	AuthorId, _, err := GetCurrentUserInfo(c)
	p.AuthorId = AuthorId
	if err != nil {
		response.FailWithMessage(constStatus.CodeNotLogin, c)
		return
	}
	if err := logic.CreateComment(p); err != nil {
		zap.L().Error("logic.CreateComment", zap.Error(err))
		response.FailWithMessage(constStatus.CodeServerBuy, c)
		return
	}
	response.OkWithMessage("success", nil, c)

}

// 评论列表
func CommentListHandler(c *gin.Context) {
	page, size := getPageInfo(c)
	data, err := logic.GetCommentList(page, size)
	if err != nil {
		zap.L().Error("logic.GetCommentList", zap.Error(err))
		response.ResponseErr(c, response.CodeServerBusy)
		return
	}
	response.ResponseSuccess(c, data)

}
